package com.example.project.util;

import cn.hutool.core.map.MapUtil;
import com.example.project.exception.BusinessException;

import java.util.Map;

public class FunctionUtil {

    public static boolean equals(Object value,Object anObject){
        if (StringUtil.isEmpty(value)||StringUtil.isEmpty(anObject))
            return false;
        return StringUtil.getValue(value).equals(anObject)
                ||IntegerUtil.getValue(value) == anObject
                ||DoubleUtil.getValue(value) == anObject;
    }

    public static boolean containsKey(Map paramMap, String paramKey){
        if (MapUtil.isEmpty(paramMap)||StringUtil.isEmpty(paramKey)) {
            return false;
        }
        return paramMap.containsKey(paramKey)&&!StringUtil.isEmpty(paramMap.get(paramKey));
    }



    public static void check(boolean flag, String msg){
        if (flag) {
            throw new BusinessException(msg);
        }
    }

    public static void check(boolean flag,Integer error, String msg){
        if (flag) {
            throw new BusinessException(error,msg);
        }
    }

    /**
     * 匹配Map对象中的参数是否符合规则
     * @param paramMap Map对象
     * @param paramKey 参数Key
     * @param matches 匹配规则
     * @return
     */
    public static boolean matches(Map paramMap,String paramKey,String matches){
        if (!paramMap.containsKey(paramKey)||StringUtil.isEmpty(paramMap.get(paramKey))) return false;
        return StringUtil.getValue(paramMap.get(paramKey)).matches(matches);
    }

    /**
     * 匹配Map对象中的参数是否符合规则
     * @param paramMap Map对象
     * @param paramKey 参数Key
     * @param matches 匹配规则
     * @param msg 匹配失败后提示
     * @return
     */
    public static void matches(Map paramMap,String paramKey,String matches,String msg){
        if (!paramMap.containsKey(paramKey)||StringUtil.isEmpty(paramMap.get(paramKey))) return;
        //如果匹配成功 !true = false / !false = true 失败
        if (!StringUtil.getValue(paramMap.get(paramKey)).matches(matches)) {
            throw new BusinessException(msg);
        }
    }

    private FunctionUtil() {
        throw new IllegalStateException("Utility class");
    }
}
